package com.ninemax.spell;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.SearchComponent;
import org.apache.solr.handler.component.SpellCheckComponent;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.response.SolrQueryResponse;

import com.ninemax.service.HttpSolr;
import com.ninemax.service.LocalSolr;

public class SpellTest {
	public static SolrServer httpServer = new HttpSolr("http://localhost:8080/solr/core1").getSolrServer();

	public List<String> getSpellCheckFromHttp(String term) {
		SolrQuery params = new SolrQuery();
		params.set("qt", "/suggest");
		params.set("q", term);
//		params.set("spellcheck", "on");
//		params.set("spellcheck.build", "true");
		List<String> suggestedList = new ArrayList<String>();
		QueryResponse qResponse;
		try {
			qResponse = httpServer.query(params);
			SpellCheckResponse spellCheckResponse = qResponse.getSpellCheckResponse();
			List<Suggestion> suggestionList = spellCheckResponse.getSuggestions();
			for (Suggestion suggestion : suggestionList) {
				suggestedList.addAll(suggestion.getAlternatives());
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		System.out.println("ss:"+suggestedList);
		return suggestedList;
	}

	private Collection<String> getSpellCheckFromCore(String keyword) {
		Collection<String> suggestion = new ArrayList<String>();
		LocalSolr solr = new LocalSolr();

		SolrCore core = solr.getCoreContainer().getCore("core1");
		SearchComponent speller = core.getSearchComponent("spellcheck");

		ModifiableSolrParams params = new ModifiableSolrParams();
		params.add(CommonParams.QT, "/spell");
		params.add(SpellCheckComponent.SPELLCHECK_BUILD, "true");
		params.add(CommonParams.Q, keyword);
		params.add(SpellCheckComponent.COMPONENT_NAME, "true");
		params.add(SpellCheckComponent.SPELLCHECK_COLLATE, "true");

		SolrRequestHandler handler = core.getRequestHandler("/spell");
		SolrQueryResponse rsp = new SolrQueryResponse();
		rsp.add("responseHeader", new SimpleOrderedMap());
		handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
		NamedList values = rsp.getValues();
		NamedList spellCheck = (NamedList) values.get("spellcheck");
		NamedList suggestions = (NamedList) spellCheck.get("suggestions");
		Boolean correctlySpelled = (Boolean) suggestions.get("correctlySpelled");
		if (correctlySpelled == null) {
			String collation = (String) suggestions.get("collation");
			suggestion.add(collation);
		}
	
		return suggestion;
	}

	public static void main(String[] args) {
			new SpellTest().getSpellCheckFromCore("niha pizz");
	}
}
